package com.foreveross.proxyip.application.offer.rest.provider;


import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.apache.cxf.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author loudyn
 * 
 */
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class RestExceptionMapper implements ExceptionMapper<RuntimeException> {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	public Response toResponse(RuntimeException e) {
		logger.error("Error occurs!", e);
		String reason = StringUtils.isEmpty(e.getMessage()) ? e.getClass().getSimpleName() : e.getMessage();
		RestResponse errorResp = new RestResponse.Builder().error().message(reason).build();
		return Response.ok(errorResp).build();
	}
}
